package cristiano.rodrigo.luiz.projetoandroidwn;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class GameDetailFragment extends Fragment {

	EditText edtTitulo;
	EditText edtCategoria;
	EditText edtDesenvolvedora;
	Game game;
	
	GameSalvoListener listener;
	
	public static GameDetailFragment novoDetalhe(Game g){
		Bundle parametros = new Bundle();
		parametros.putSerializable("game", g);
		
		GameDetailFragment detalhe = new GameDetailFragment();
		detalhe.setArguments(parametros);
		return detalhe;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
			
		    View layout = inflater.inflate(R.layout.activity_fragment_game_detail, null);
			
			edtTitulo = (EditText)layout.findViewById(R.id.editTextGameTitle);
			edtCategoria  = (EditText)layout.findViewById(R.id.editTextGameCategory);
			edtDesenvolvedora    = (EditText)layout.findViewById(R.id.editTextGameDeveloper);
			
			game = (Game)getArguments().getSerializable("carro");
			edtTitulo.setText(game.titulo);
			edtCategoria.setText(game.categoria);
			edtDesenvolvedora.setText(game.desenvolvedora);
			
			return layout;
	}
	
	public void salvar(){
		game.titulo = edtTitulo.getText().toString();
		game.categoria = edtCategoria.getText().toString();
		game.desenvolvedora = edtDesenvolvedora.getText().toString();
	
		GamesDB gamesDB = new GamesDB(getActivity());
		if(game.id == 0){
			gamesDB.inserir(game);
		}
		else{
			gamesDB.atualizar(game);
		}
		
		if(listener == null){
			listener.aoSalvarGame(game);
		}
	}
	
	public void setListener(GameSalvoListener listener) {
		this.listener = listener;
	}
	
	public interface GameSalvoListener{
		void aoSalvarGame(Game g);
	}
	
}
